package com.ht.api.call.func.impl.ad;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 编辑event_ad_config的后置函数
 * @author asq
 * @createTime 2025年3月20日 10:05:40
 */
@Slf4j
public class EventAdConfigEditAfterFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "编辑event_ad_config的后置函数";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		String configId = param.getStr("main_id");
		DbList eventPlanList = Db.createSqlParser("event_ad_plan_config").where("config_id", configId).query(me.getDataSource());
		List<Entity> dataList = new ArrayList<>();
		for (Entity eventPlan : eventPlanList) {
			String planId = eventPlan.getStr("plan_id");
			Entity plan = Entity.create("ad_plan");
			plan.set("main_id", planId);
			plan.set("audience_rule", param.get("audience_rule"));
			plan.set("app_rule", param.get("app_rule"));

			dataList.add(plan);
		}

		Db.insertBatchWithStream(me.getStreamLoadConfig(), dataList);

		return JSONUtil.createObj();
	}
}
